<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>main.cpp 示例文件 | Qt Widgets 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="qtwidgets-statemachine-trafficlight-example.html">Traffic Light Example</a></td><td >main.cpp 示例文件</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">main.cpp 示例文件</h1>
<span class="subtitle">statemachine/trafficlight/main.cpp</span>
<!-- $$$statemachine/trafficlight/main.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp">

  <span class="comment">/****************************************************************************
  **
  ** Copyright (C) 2016 The Qt Company Ltd.
  ** Contact: https://www.qt.io/licensing/
  **
  ** This file is part of the QtCore module of the Qt Toolkit.
  **
  ** $QT_BEGIN_LICENSE:BSD$
  ** Commercial License Usage
  ** Licensees holding valid commercial Qt licenses may use this file in
  ** accordance with the commercial license agreement provided with the
  ** Software or, alternatively, in accordance with the terms contained in
  ** a written agreement between you and The Qt Company. For licensing terms
  ** and conditions see https://www.qt.io/terms-conditions. For further
  ** information use the contact form at https://www.qt.io/contact-us.
  **
  ** BSD License Usage
  ** Alternatively, you may use this file under the terms of the BSD license
  ** as follows:
  **
  ** &quot;Redistribution and use in source and binary forms, with or without
  ** modification, are permitted provided that the following conditions are
  ** met:
  **   * Redistributions of source code must retain the above copyright
  **     notice, this list of conditions and the following disclaimer.
  **   * Redistributions in binary form must reproduce the above copyright
  **     notice, this list of conditions and the following disclaimer in
  **     the documentation and/or other materials provided with the
  **     distribution.
  **   * Neither the name of The Qt Company Ltd nor the names of its
  **     contributors may be used to endorse or promote products derived
  **     from this software without specific prior written permission.
  **
  **
  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
  **
  ** $QT_END_LICENSE$
  **
  ****************************************************************************/</span>

  <span class="preprocessor">#include &lt;QtWidgets&gt;</span>

  <span class="keyword">class</span> LightWidget : <span class="keyword">public</span> <span class="type"><a href="qwidget.html">QWidget</a></span>
  {
      Q_OBJECT
      Q_PROPERTY(bool on READ isOn WRITE setOn)
  <span class="keyword">public</span>:
      LightWidget(<span class="keyword">const</span> <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> <span class="operator">&amp;</span>color<span class="operator">,</span> <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>)
          : <span class="type"><a href="qwidget.html">QWidget</a></span>(parent)<span class="operator">,</span> m_color(color)<span class="operator">,</span> m_on(<span class="keyword">false</span>) {}

      bool isOn() <span class="keyword">const</span>
          { <span class="keyword">return</span> m_on; }
      <span class="type">void</span> setOn(bool on)
      {
          <span class="keyword">if</span> (on <span class="operator">=</span><span class="operator">=</span> m_on)
              <span class="keyword">return</span>;
          m_on <span class="operator">=</span> on;
          update();
      }

  <span class="keyword">public</span> <span class="keyword">slots</span>:
      <span class="type">void</span> turnOff() { setOn(<span class="keyword">false</span>); }
      <span class="type">void</span> turnOn() { setOn(<span class="keyword">true</span>); }

  <span class="keyword">protected</span>:
      <span class="type">void</span> paintEvent(<span class="type"><a href="../qtgui/qpaintevent.html">QPaintEvent</a></span> <span class="operator">*</span>) override
      {
          <span class="keyword">if</span> (<span class="operator">!</span>m_on)
              <span class="keyword">return</span>;
          <span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> painter(<span class="keyword">this</span>);
          painter<span class="operator">.</span>setRenderHint(<span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span><span class="operator">::</span>Antialiasing);
          painter<span class="operator">.</span>setBrush(m_color);
          painter<span class="operator">.</span>drawEllipse(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> width()<span class="operator">,</span> height());
      }

  <span class="keyword">private</span>:
      <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> m_color;
      bool m_on;
  };

  <span class="keyword">class</span> TrafficLightWidget : <span class="keyword">public</span> <span class="type"><a href="qwidget.html">QWidget</a></span>
  {
  <span class="keyword">public</span>:
      TrafficLightWidget(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>)
          : <span class="type"><a href="qwidget.html">QWidget</a></span>(parent)
      {
          <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span> <span class="operator">*</span>vbox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span>(<span class="keyword">this</span>);
          m_red <span class="operator">=</span> <span class="keyword">new</span> LightWidget(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>red);
          vbox<span class="operator">-</span><span class="operator">&gt;</span>addWidget(m_red);
          m_yellow <span class="operator">=</span> <span class="keyword">new</span> LightWidget(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>yellow);
          vbox<span class="operator">-</span><span class="operator">&gt;</span>addWidget(m_yellow);
          m_green <span class="operator">=</span> <span class="keyword">new</span> LightWidget(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>green);
          vbox<span class="operator">-</span><span class="operator">&gt;</span>addWidget(m_green);
          <span class="type"><a href="../qtgui/qpalette.html">QPalette</a></span> pal <span class="operator">=</span> palette();
          pal<span class="operator">.</span>setColor(<span class="type"><a href="../qtgui/qpalette.html">QPalette</a></span><span class="operator">::</span>Background<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>black);
          setPalette(pal);
          setAutoFillBackground(<span class="keyword">true</span>);
      }

      LightWidget <span class="operator">*</span>redLight() <span class="keyword">const</span>
          { <span class="keyword">return</span> m_red; }
      LightWidget <span class="operator">*</span>yellowLight() <span class="keyword">const</span>
          { <span class="keyword">return</span> m_yellow; }
      LightWidget <span class="operator">*</span>greenLight() <span class="keyword">const</span>
          { <span class="keyword">return</span> m_green; }

  <span class="keyword">private</span>:
      LightWidget <span class="operator">*</span>m_red;
      LightWidget <span class="operator">*</span>m_yellow;
      LightWidget <span class="operator">*</span>m_green;
  };

  <span class="type"><a href="../qtcore/qstate.html">QState</a></span> <span class="operator">*</span>createLightState(LightWidget <span class="operator">*</span>light<span class="operator">,</span> <span class="type">int</span> duration<span class="operator">,</span> <span class="type"><a href="../qtcore/qstate.html">QState</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>)
  {
      <span class="type"><a href="../qtcore/qstate.html">QState</a></span> <span class="operator">*</span>lightState <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtcore/qstate.html">QState</a></span>(parent);
      <span class="type"><a href="../qtcore/qtimer.html">QTimer</a></span> <span class="operator">*</span>timer <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtcore/qtimer.html">QTimer</a></span>(lightState);
      timer<span class="operator">-</span><span class="operator">&gt;</span>setInterval(duration);
      timer<span class="operator">-</span><span class="operator">&gt;</span>setSingleShot(<span class="keyword">true</span>);
      <span class="type"><a href="../qtcore/qstate.html">QState</a></span> <span class="operator">*</span>timing <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtcore/qstate.html">QState</a></span>(lightState);
      <span class="type"><a href="../qtcore/qobject.html">QObject</a></span><span class="operator">::</span>connect(timing<span class="operator">,</span> SIGNAL(entered())<span class="operator">,</span> light<span class="operator">,</span> SLOT(turnOn()));
      <span class="type"><a href="../qtcore/qobject.html">QObject</a></span><span class="operator">::</span>connect(timing<span class="operator">,</span> SIGNAL(entered())<span class="operator">,</span> timer<span class="operator">,</span> SLOT(start()));
      <span class="type"><a href="../qtcore/qobject.html">QObject</a></span><span class="operator">::</span>connect(timing<span class="operator">,</span> SIGNAL(exited())<span class="operator">,</span> light<span class="operator">,</span> SLOT(turnOff()));
      <span class="type"><a href="../qtcore/qfinalstate.html">QFinalState</a></span> <span class="operator">*</span>done <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtcore/qfinalstate.html">QFinalState</a></span>(lightState);
      timing<span class="operator">-</span><span class="operator">&gt;</span>addTransition(timer<span class="operator">,</span> SIGNAL(timeout())<span class="operator">,</span> done);
      lightState<span class="operator">-</span><span class="operator">&gt;</span>setInitialState(timing);
      <span class="keyword">return</span> lightState;
  }

  <span class="keyword">class</span> TrafficLight : <span class="keyword">public</span> <span class="type"><a href="qwidget.html">QWidget</a></span>
  {
  <span class="keyword">public</span>:
      TrafficLight(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>)
          : <span class="type"><a href="qwidget.html">QWidget</a></span>(parent)
      {
          <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span> <span class="operator">*</span>vbox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span>(<span class="keyword">this</span>);
          TrafficLightWidget <span class="operator">*</span>widget <span class="operator">=</span> <span class="keyword">new</span> TrafficLightWidget();
          vbox<span class="operator">-</span><span class="operator">&gt;</span>addWidget(widget);
          vbox<span class="operator">-</span><span class="operator">&gt;</span>setMargin(<span class="number">0</span>);

          <span class="type"><a href="../qtcore/qstatemachine.html">QStateMachine</a></span> <span class="operator">*</span>machine <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtcore/qstatemachine.html">QStateMachine</a></span>(<span class="keyword">this</span>);
          <span class="type"><a href="../qtcore/qstate.html">QState</a></span> <span class="operator">*</span>redGoingYellow <span class="operator">=</span> createLightState(widget<span class="operator">-</span><span class="operator">&gt;</span>redLight()<span class="operator">,</span> <span class="number">3000</span>);
          redGoingYellow<span class="operator">-</span><span class="operator">&gt;</span>setObjectName(<span class="string">&quot;redGoingYellow&quot;</span>);
          <span class="type"><a href="../qtcore/qstate.html">QState</a></span> <span class="operator">*</span>yellowGoingGreen <span class="operator">=</span> createLightState(widget<span class="operator">-</span><span class="operator">&gt;</span>yellowLight()<span class="operator">,</span> <span class="number">1000</span>);
          yellowGoingGreen<span class="operator">-</span><span class="operator">&gt;</span>setObjectName(<span class="string">&quot;yellowGoingGreen&quot;</span>);
          redGoingYellow<span class="operator">-</span><span class="operator">&gt;</span>addTransition(redGoingYellow<span class="operator">,</span> SIGNAL(finished())<span class="operator">,</span> yellowGoingGreen);
          <span class="type"><a href="../qtcore/qstate.html">QState</a></span> <span class="operator">*</span>greenGoingYellow <span class="operator">=</span> createLightState(widget<span class="operator">-</span><span class="operator">&gt;</span>greenLight()<span class="operator">,</span> <span class="number">3000</span>);
          greenGoingYellow<span class="operator">-</span><span class="operator">&gt;</span>setObjectName(<span class="string">&quot;greenGoingYellow&quot;</span>);
          yellowGoingGreen<span class="operator">-</span><span class="operator">&gt;</span>addTransition(yellowGoingGreen<span class="operator">,</span> SIGNAL(finished())<span class="operator">,</span> greenGoingYellow);
          <span class="type"><a href="../qtcore/qstate.html">QState</a></span> <span class="operator">*</span>yellowGoingRed <span class="operator">=</span> createLightState(widget<span class="operator">-</span><span class="operator">&gt;</span>yellowLight()<span class="operator">,</span> <span class="number">1000</span>);
          yellowGoingRed<span class="operator">-</span><span class="operator">&gt;</span>setObjectName(<span class="string">&quot;yellowGoingRed&quot;</span>);
          greenGoingYellow<span class="operator">-</span><span class="operator">&gt;</span>addTransition(greenGoingYellow<span class="operator">,</span> SIGNAL(finished())<span class="operator">,</span> yellowGoingRed);
          yellowGoingRed<span class="operator">-</span><span class="operator">&gt;</span>addTransition(yellowGoingRed<span class="operator">,</span> SIGNAL(finished())<span class="operator">,</span> redGoingYellow);

          machine<span class="operator">-</span><span class="operator">&gt;</span>addState(redGoingYellow);
          machine<span class="operator">-</span><span class="operator">&gt;</span>addState(yellowGoingGreen);
          machine<span class="operator">-</span><span class="operator">&gt;</span>addState(greenGoingYellow);
          machine<span class="operator">-</span><span class="operator">&gt;</span>addState(yellowGoingRed);
          machine<span class="operator">-</span><span class="operator">&gt;</span>setInitialState(redGoingYellow);
          machine<span class="operator">-</span><span class="operator">&gt;</span>start();
      }
  };

  <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span><span class="operator">*</span>argv)
  {
      <span class="type"><a href="qapplication.html">QApplication</a></span> app(argc<span class="operator">,</span> argv);

      TrafficLight widget;
      widget<span class="operator">.</span>resize(<span class="number">110</span><span class="operator">,</span> <span class="number">300</span>);
      widget<span class="operator">.</span>show();

      <span class="keyword">return</span> app<span class="operator">.</span>exec();
  }

  <span class="preprocessor">#include &quot;main.moc&quot;</span>

</pre>
</div>
<!-- @@@statemachine/trafficlight/main.cpp -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
